// pages/index/index.js
var config = require('../../config');
var util = require('../../utils/util.js');
var app = getApp();
// 开发时用本地数据测试-调用接口查询轮播图数据和推荐数据
var index_data = {};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: config.service.host + '/',
    // 轮播图图片
    swiperList: [], // index_data.swiperList
    // 卡片数据
    recommendList: [], // index_data.recommendList
    // 记录轮播图展示的图片,默认0
    currentPicIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    app.getUserInfo(function () {
      wx.request({
        url: config.service.index_data,
        data: { istest: app.globalData.testUser },
        method: "POST",
        success(result) {
          wx.hideToast();
          index_data.swiperList = result.data.filter(function (item) {
            return item.swiper == 1;
          });
          index_data.recommendList = result.data.filter(function (item) {
            return item.recommend == 1;
          });
          var l = index_data.recommendList.length;
          var times = 3 - l % 3;
          times = times === 3 ? 0 : times;
          for (var i = 0; i < times; i++) {
            index_data.recommendList.push({
              id: null
            });
          }
          self.setData({
            swiperList: index_data.swiperList,
            recommendList: index_data.recommendList
          });
        },
        fail(error) {
          util.showModel('请求失败，请重试！', error);
        }
      });
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '制定美好绚丽的电子贺卡'
    }
  },

  /**
   * 轮播图切换事件
   */
  changePic: function (e) {
    this.setData({
      currentPicIndex: e.detail.current
    })
  },

  /**
   * 点击轮播图图片事件
   */
  swipclick: function () {
    var card = this.data.swiperList[this.data.currentPicIndex];
    app.globalData.cardInfo = card;
    wx.navigateTo({
      url: `../${card.style}/card`
    });
  },

  /**
   * 更多的点击事件
   */
  moreclick: function () {
    wx.switchTab({
      url: '../kind/kind'
    });
  },

  /**
   * 点击卡片
   */
  cardClik: function (e) {
    var card = this.data.recommendList[e.currentTarget.dataset.index];
    app.globalData.cardInfo = card;
    wx.navigateTo({
      url: `../${card.style}/card`
    });
  }
})